<html>
<head><meta charset="utf-8"><title>Type defined by stdlib · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html">Type defined by stdlib</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221823765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221823765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221823765">(Jan 06 2021 at 21:07)</a>:</h4>
<p>Is there an easy way to see if a type is defined in standard library. I'm trying to see  if the type implementing Drop is defined by the user and only then do more work</p>



<a name="221824077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221824077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221824077">(Jan 06 2021 at 21:10)</a>:</h4>
<p>Current thoughts are to annotate each type/drop impl in stdlib with some attribute and check for the presence of that. But I'm trying to see if I can make it future proof and also avoid all the work of annotating all the types <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="221839010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221839010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221839010">(Jan 06 2021 at 23:41)</a>:</h4>
<p>Is "by the user" really what you want?  Often "defined in the current crate" is more helpful, if you're suggesting edits to something...</p>



<a name="221839504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221839504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221839504">(Jan 06 2021 at 23:47)</a>:</h4>
<p>that could also work. Basically I'm defining a lint for code that would break when RFC 2229 feature is enabled and trying provide migration suggestions.  We only want to do this for types that impl Drop and are not in stdlib.</p>



<a name="221844173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221844173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221844173">(Jan 07 2021 at 00:47)</a>:</h4>
<p>can you just write the lint regardless, and <code>#![allow(...)]</code> it in stdlib?</p>



<a name="221845164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221845164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221845164">(Jan 07 2021 at 01:03)</a>:</h4>
<p>I can see the confusion <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>
<p>An example will probably help here</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">S1</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">x</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">y</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">UserDropType</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">UserDropType</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">S2</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">x</span>: <span class="nc">UserDropType</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">y</span>: <span class="nc">UserDropType</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">S3</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">x</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">y</span>: <span class="nc">UserDropType</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">S4</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">x</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">y</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="n">fro</span><span class="w"> </span><span class="n">S4</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>So depending on how instances of S2-4 are used, I might want to provide migrations. </p>
<p>Eg: </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">s3</span>: <span class="nc">S3</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">c1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">s3</span><span class="p">.</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">s3</span>: <span class="nc">S3</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">c2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">s3</span><span class="p">.</span><span class="n">y</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>When we only capture disjoint fields i.e. only <code>s3.x</code> in c1 and <code>s3.y</code> in <code>c2</code>, drop order changes for the other field in s3 (since without the feature s3 is captured completely and all captured and their fields drop when the closure is dropped). </p>
<p>Now in the case of c2 <code>s3.x: String</code> is being affected, which has Drop implemented in stdlib, so we don't want to migrate for it -- we know it will only release memory. However, we don't know what to expect from user-defined Drops and in the case of <code>c1</code> when <code>s3.y: UserDropType</code> is dropped is changed so we want to provide migration suggestion to maintain original program behavior</p>



<a name="221848523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221848523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221848523">(Jan 07 2021 at 01:58)</a>:</h4>
<p>I see, so what you want is a user-defined closure where drop order will change, but only when dropping user types, because you consider <code>std</code> types as "vetted", unproblematic</p>



<a name="221848585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221848585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221848585">(Jan 07 2021 at 01:59)</a>:</h4>
<p>(I have no involvement in that RFC, but my quick take is that I would <em>not</em> let it affect drop order at all.)</p>



<a name="221848668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221848668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221848668">(Jan 07 2021 at 02:01)</a>:</h4>
<p>(similar to the way that NLL-refined borrow checking had no effect on <em>when</em> something is actually dropped)</p>



<a name="221848779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221848779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221848779">(Jan 07 2021 at 02:03)</a>:</h4>
<p>(but this isn't the place to argue that... just take this as a generic user reaction to think about, I suppose)</p>



<a name="221848960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221848960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221848960">(Jan 07 2021 at 02:06)</a>:</h4>
<p>Yes should Drop order change is an unresolved question in the RFC as it stands. Because of the 2021 edition release being close, it seemed like a good time to have a breaking change and provide migration support accordingly.  We discussed amongst rfc-2229-wg that we should go ahead and implement the lint, but more lang-team decisions are pending/to-be scheduled once people are back from holidays.</p>
<p>That aside yes the problem is that we want to migrate if a variable of a type defined by the user will be dropped at a different time compared to if the feature isn't enabled. I have a basic working solution for figuring most of the details except if the type is a user-defined one.</p>



<a name="221860746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221860746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221860746">(Jan 07 2021 at 06:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Type.20defined.20by.20stdlib/near/221839010">said</a>:</p>
<blockquote>
<p>Is "by the user" really what you want?  Often "defined in the current crate" is more helpful, if you're suggesting edits to something...</p>
</blockquote>
<p>I'm assuming this would solve my problem. How do I check for this <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span>?</p>



<a name="221863003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221863003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221863003">(Jan 07 2021 at 07:07)</a>:</h4>
<p>actually, that will not work, it could be user-defined type just not in the current crate.</p>



<a name="221919117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221919117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221919117">(Jan 07 2021 at 08:56)</a>:</h4>
<p>You could look at the CrateNum of the DefId, then look up its name with tcx.crate_name(). That's not 100% foolproof but I wouldn't expect anyone to name a crate <code>std</code>.</p>



<a name="221922468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221922468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221922468">(Jan 07 2021 at 09:45)</a>:</h4>
<p>Is that legal to do with <code>#[no_std]</code> ?</p>



<a name="221923234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20defined%20by%20stdlib/near/221923234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20defined.20by.20stdlib.html#221923234">(Jan 07 2021 at 09:56)</a>:</h4>
<p>It's certainly legal, it just won't work <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> to support no_std you should also check for <code>core</code> and <code>alloc</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>